﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Configuration;
using System.Threading;

namespace _4591BRD
{
    class DataFileReader
    {
        private string file;
        private string dateStr;

        //private volatile bool _shouldStop;
        //public void RequestStop()
        //{
        //    _shouldStop = true;
        //}

        public DataFileReader(string strFile, string strDate)
        {
            file = strFile;
            dateStr = strDate;
        }

        public void Run()
        {
            //Log.WriteLine(1, "Start Read file: " + file);
            Common.AddThreadCount();

            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader reader = new StreamReader(fs);

            string battlefieldid = "0";
            int e1 = file.IndexOf("BattleRecord");
            if (e1 > 0)
            {
                e1 += 12;
                int e2 = file.IndexOf(" ", e1);
                battlefieldid = file.Substring(e1, e2 - e1);
            }

            int bid = int.Parse(battlefieldid);
            if (bid > 60000 && bid < 70000)
            {
                //read every line in the file
                string logLine = reader.ReadLine();
                while (logLine != null)
                {
                    ParseLogLine(logLine);

                    logLine = reader.ReadLine();
                }
                reader.Close();
                fs.Close();
            }
            else
                Log.WriteError("BattlefieldID NOT 6XXXXX!");

            //线程工作结束
            //Log.WriteLine(1, "Finish Read file: " + file);
            Common.RemoveThreadCount();
        }


        //处理每一行
        private void ParseLogLine(string logLine)
        {
            //Log.WriteLine(logLine);
            string time = "", locX = "0", locY = "0", locZ = "0";
            string strCharid = "0", eventid = "0", eventdata = "";

            string[] sKeys = logLine.Split(' ');

            if (sKeys.Length > 6)
            {
                time = sKeys[0] + " " + sKeys[1];
                locX = sKeys[2];
                locY = sKeys[3];
                locZ = sKeys[4];
                strCharid = sKeys[5];
                eventid = sKeys[6];
                int charid = int.Parse(strCharid);

                for (int i = 7; i < sKeys.GetLength(0); i++)
                    eventdata += " " + sKeys[i];
                eventdata = eventdata.Trim();

                if (eventid != "")
                {
                    switch (eventid)
                    {
                        case "10000": //服务器启动

                            break;
                        case "20000": //服务器关闭

                            break;
                        case "30000": //角色进入
                            CurrentDateManager.Instance.AddEnterBG(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "40000": //角色登出

                            break;
                        case "0": //变更功勋
                            CurrentDateManager.Instance.AddPoint(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "1": //击杀机器人
                            CurrentDateManager.Instance.AddKill(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "2": //击毁设施
                            CurrentDateManager.Instance.AddKillVehicle(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "3": //死亡
                            CurrentDateManager.Instance.AddDeath(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "4": //更换装备
                            CurrentDateManager.Instance.Spawn(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "5": //使用武器
                            CurrentDateManager.Instance.UseWeapon(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "6": //使用道具
                            CurrentDateManager.Instance.UseEquip(charid, locX, locY, locZ, time, eventdata);
                            break;
                        case "7": //进入载具设施

                            break;
                        case "8": //离开载具设施

                            break;
                        case "9": //更换座位

                            break;
                        case "10": //使用功能设施

                            break;
                        case "11": //加入小队
                            CurrentDateManager.Instance.AddTeamGroup(charid, locX, locY, locZ, time, 11);
                            break;
                        case "12": //离开小队
                            CurrentDateManager.Instance.AddTeamGroup(charid, locX, locY, locZ, time, 12);
                            break;
                        case "13": //担任小队队长

                            break;
                        case "14": //破解主控电脑

                            break;
                        case "15": //担任指挥官
                            CurrentDateManager.Instance.AddTeamGroup(charid, locX, locY, locZ, time, 15);
                            break;
                        case "16": //离职指挥官
                            CurrentDateManager.Instance.AddTeamGroup(charid, locX, locY, locZ, time, 16);
                            break;
                        case "17": //领取载具

                            break;
                        case "18": //恶意击杀惩罚

                            break;
                        case "19": //担任小队长
                            CurrentDateManager.Instance.AddTeamGroup(charid, locX, locY, locZ, time, 19);
                            break;
                        case "20": //离职小队长
                            CurrentDateManager.Instance.AddTeamGroup(charid, locX, locY, locZ, time, 20);
                            break;
                        default:
                            Log.WriteError("Wrong EventID : " + logLine);
                            break;
                    }
                }
                else
                    Log.WriteError("EventID is NULL : " + logLine);
            }
            else
                Log.WriteError("Wrong Log String : " + logLine);
        }
    }
}
